package com.wisdytech.linkdcs.report.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageInfo;
import com.wisdytech.common.bean.Filter;
import com.wisdytech.common.bean.StandardResult;
import com.wisdytech.common.bean.StandardTableResult;
import com.wisdytech.common.enums.EquipmentStatus;
import com.wisdytech.common.handler.BaseService;
import com.wisdytech.common.utils.BaseUtil;
import com.wisdytech.common.utils.DateUtil;
import com.wisdytech.linkdcs.equipment.dao.IEqAlarmRecordDao;
import com.wisdytech.linkdcs.equipment.dao.IEqEquipmentDao;
import com.wisdytech.linkdcs.equipment.dao.IEqStatusDao;
import com.wisdytech.linkdcs.equipment.model.Equipment;
import com.wisdytech.linkdcs.equipment.model.Status;
import com.wisdytech.linkdcs.report.dto.HistorySearchDTO;
import com.wisdytech.linkdcs.report.service.IReportService;
import com.wisdytech.linkdcs.report.vo.AlarmHistoryAnalysisVO;
import com.wisdytech.linkdcs.report.vo.AlarmHistoryVO;
import com.wisdytech.linkdcs.report.vo.StopHistoryAnalysisVO;
import com.wisdytech.linkdcs.report.vo.StopHistoryVO;

/**
 * 
 * <p>ClassName: ReportServiceImpl</p>
 * <p>Description: 报表实现类</p>
 * <p>Author: PC</p>
 * <p>Date: 2017-11-10</p>
 */
@Service
@Transactional
public class ReportServiceImpl  extends BaseService<IEqEquipmentDao, Equipment> implements IReportService {
    
    @Resource
    private IEqEquipmentDao equipmentDao;
    
    @Resource
    private IEqStatusDao statusDao;
    
    @Resource
    private IEqAlarmRecordDao alarmRecordDao;

    @Override
    public StandardTableResult<StopHistoryVO> listStopHistory(Filter filter,HistorySearchDTO search) {
        
        StandardTableResult<StopHistoryVO> result =new StandardTableResult<>();
        List<StopHistoryVO> resultList=new ArrayList<>();
        
        Map<String,Object> countParams=new HashMap<>();
//        countParams.put("startTime", search.getStartTime());
//        countParams.put("endTime", search.getEndTime());
        
        Map<String,Object> params =new HashMap<>();
        
        params.put("eqCode", search.getEqCode());
        params.put("operationId", search.getOperationId());
        
        List<Equipment> lists =
                super.beforeList(IEqEquipmentDao.class, filter).listDetail(params);
        
        PageInfo<Equipment> pageInfo=new PageInfo<>(lists);
        
        for(Equipment equipment : lists){

            countParams.put("eqId", equipment.getPkId());
            countParams.put("eqStatus", EquipmentStatus.UNUSUAL.getCode());
//            Long count =statusDao.countEqStopTime(countParams);
            //查询所有停机数据
            List<Status> listStatus = statusDao.selectEqStopTime(countParams);
            long count=0;
            for(Status status : listStatus) {
            	long start = status.getStartTime().getTime();//开始时间
            	long end = status.getEndTime().getTime();//结束时间
            	long startSearch = Long.parseLong(search.getStartTime());//查询开始时间
            	long endSearch = Long.parseLong(search.getEndTime());//查询开始时间
            	if(start<startSearch && end > startSearch) {
            		long count1 = end - startSearch;
            		count = count +count1;
            	}
            	if(start>startSearch && end < endSearch) {
            		long count2 = end - start;
            		count = count +count2;
            	}
            	if(start<endSearch && end > endSearch) {
            		long count3 = end - endSearch;
            		count = count +count3;
            	}
            }
            
            StopHistoryVO vo =new StopHistoryVO();
            vo.setEqCode(equipment.getEquipCode());
            vo.setEqName(equipment.getEquipName());
            vo.setEqType(equipment.getModelType());
            vo.setOpName(equipment.getOperationName());
            vo.setTime(String.valueOf(DateUtil.second2Hour(count, 2)));
            resultList.add(vo);
        }
        
        result.setRows(resultList);
        result.setTotal(pageInfo.getTotal());
        
        return result;
    }

    @Override
    public StandardResult listStopHistoryAnalysis(HistorySearchDTO search) {
        
        StopHistoryAnalysisVO vo =new StopHistoryAnalysisVO();
        
        Map<String,String> params =new HashMap<>();  
        params.put("opId", search.getOperationId());
        params.put("eqCode", search.getEqCode());
//        params.put("startTime", search.getStartTime());
//        params.put("endTime", search.getEndTime());
        
        List<Status>  result =
                statusDao.countEqStopAnalysis(params);
        
        List<String> x=new ArrayList<>();
        List<String> y=new ArrayList<>();
        
        for(int i=0;i<result.size();i++) {
        	String key = result.get(i).getOperationName();//工序
        	long count=0;//停机时长
        	for(int j=1;j<result.size();j++) {
        		if(result.get(i).getOperationName().equals(result.get(j).getOperationName())) {
                    Date startTime =result.get(j).getStartTime();
                    Date endTime =result.get(j).getEndTime();
                	long start = startTime.getTime();//开始时间
                	long end = endTime.getTime();//结束时间
                	long startSearch = Long.parseLong(search.getStartTime());//查询开始时间
                	long endSearch = Long.parseLong(search.getEndTime());//查询开始时间
                	if(start<startSearch && end > startSearch) {
                		long count1 = end - startSearch;
                		count = count +count1;
                	}
                	if(start>startSearch && end < endSearch) {
                		long count2 = end - start;
                		count = count +count2;
                	}
                	if(start<endSearch && end > endSearch) {
                		long count3 = end - endSearch;
                		count = count +count3;
                	}
        			result.remove(result.get(j));//移除相同工序的数据
        		}
        	}
        	
        	
            x.add((String) key);
            y.add(String.valueOf(DateUtil.second2Hour(count, 2)));
        }
        
        vo.setX(x);
        vo.setY(y);
        
        return StandardResult.getSuccessResult(vo);
    }

    @Override
    public StandardTableResult<AlarmHistoryVO> listAlarmHistory(Filter filter, HistorySearchDTO search) {
        StandardTableResult<AlarmHistoryVO> result =new StandardTableResult<>();
        List<AlarmHistoryVO> resultList=new ArrayList<>();
        
        Map<String,String> countParams=new HashMap<>();
        countParams.put("startTime", search.getStartTime());
        countParams.put("endTime", search.getEndTime());
        
        Map<String,Object> params =new HashMap<>();
        
        params.put("eqCode", search.getEqCode());
        params.put("operationId", search.getOperationId());
        
        List<Equipment> lists =
                super.beforeList(IEqEquipmentDao.class, filter).listDetail(params);
        
        PageInfo<Equipment> pageInfo=new PageInfo<>(lists);
        
        for(Equipment equipment : lists){

            countParams.put("eqId", equipment.getPkId());
            
            Map<String, Object> count =alarmRecordDao.countEqAlarmTime(countParams);
            
            
            //统计设备预警次数
            BigDecimal num =(BigDecimal) count.get("count");
            //统计设备处理次数
            BigDecimal success =(BigDecimal) count.get("success");
            //统计设备未处理次数 
            BigDecimal notProcessed =num.subtract(success);
            AlarmHistoryVO vo =new AlarmHistoryVO();
            vo.setEqCode(equipment.getEquipCode());
            vo.setEqName(equipment.getEquipName());
            vo.setEqType(equipment.getModelType());
            vo.setOpName(equipment.getOperationName());
            vo.setNum(String.valueOf(num));
            vo.setProcessed(String.valueOf(success));
            vo.setNotProcessed(String.valueOf(notProcessed));
            
            
//            vo.setNum((String) count.get("count"));
//            vo.setProcessed(processed)
            
            
//            vo.setTime(String.valueOf(DateUtil.second2Hour((Long) count.get("time"), 2)));
            
            
            resultList.add(vo);
        }
        
        result.setRows(resultList);
        result.setTotal(pageInfo.getTotal());
        
        return result;
    }

    @Override
    public StandardResult listAlarmHistoryAnalysis(HistorySearchDTO search) {
            AlarmHistoryAnalysisVO vo =new AlarmHistoryAnalysisVO();
            
            Map<String,String> params =new HashMap<>();  
            params.put("opId", search.getOperationId());
            params.put("eqCode", search.getEqCode());
            params.put("startTime", search.getStartTime());
            params.put("endTime", search.getEndTime());
            
            List<Map<String, Object>>  result =
                    alarmRecordDao.countEqAlarmAnalysis(params);
            
            
            List<String> x=new ArrayList<>();
            //预警次数
            List<String> y1=new ArrayList<>();
            //预警处理率
            List<String> y2=new ArrayList<>();
            
            long y1Max =0l;
            double y2Max =0.00;
            
            
            for(Map<String,Object>  rMap : result){
                
                
                Object key =rMap.get("name");
                x.add((String) key);
                
                long num =(long) rMap.get("count");
                //查找y1最大值
                if(y1Max<num){
                    y1Max=num;
                }
                
                
                long success =(long) rMap.get("success");
                
                double proportion = BaseUtil.divide(success, num, 4);
                //查找y2最大值
                if(y2Max<proportion){
                    y2Max =proportion;
                }
                
                
                y1.add(String.valueOf(num));
                y2.add(String.valueOf(proportion*100));
                
            }
            
            
            vo.setX(x);
            vo.setY1(y1);
            vo.setY2(y2);
            vo.setY1Max(String.valueOf(y1Max*1.2));
            vo.setY2Max(String.valueOf(100));
            vo.setY1Min("0");
            vo.setY2Min("0");
            
            
            return StandardResult.getSuccessResult(vo);
    }

}
